package com.example.demo.model.core;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * 响应信息主体
 */
@NoArgsConstructor
@Getter
@Setter
public class R<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    private String code;

    private String msg;

    private T data;

    public static <T> R<T> ok() {
        return restResult(null, ResultCode.SUCCESS.getCode(), "操作成功");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultCode.SUCCESS.getCode(), "操作成功");
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, ResultCode.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> ok(String msg, T data) {
        return restResult(data, ResultCode.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, ResultCode.SYSTEM_ERROR.getCode(), "操作失败");
    }

    public static <T> R<T> fail(IResultCode resultCode) {
        return restResult(null, resultCode.getCode(), resultCode.getMsg());
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, ResultCode.SYSTEM_ERROR.getCode(), msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, ResultCode.SYSTEM_ERROR.getCode(), "操作失败");
    }

    public static <T> R<T> fail(String msg, T data) {
        return restResult(data, ResultCode.SYSTEM_ERROR.getCode(), msg);
    }

    public static <T> R<T> fail(String code, String msg) {
        return restResult(null, code, msg);
    }


    private static <T> R<T> restResult(T data, String code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }


}
